package com.gydz.gydzbaseframeworkboot.SgsE.service.impl;

import com.gydz.gydzbaseframeworkboot.SgsE.dao.YingSheMapper;
import com.gydz.gydzbaseframeworkboot.SgsE.domain.YingShe;
import com.gydz.gydzbaseframeworkboot.SgsE.service.YingSheService;
import com.gydz.gydzbaseframeworkboot.SgsE.util.InterpretResultsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class YingSheServiceImpl implements YingSheService {

    @Autowired
    YingSheMapper yingSheMapper;

    private static int mapNum = 0;
    private static Map<String,Map<String,String>> codingMap = new HashMap<>();

    private void setMap(){
        if(mapNum==0){
            //获取不同静态表数据
            Map<String, String> zhongDuanMap = getMiddleMap("zhong_duan").get("map");
            Map<String, String> ascMaBiaoMap = getMiddleMap("asc_ma_biao").get("map");
            Map<String, String> huJiaoLeiXingMap = getMiddleMap("hu_jiao_lei_xing").get("map");
            Map<String, String> duanHaoMaMap = getMiddleMap("duan_hao_ma").get("map");
            Map<String, String> gongNengMaMap = getMiddleMap("gong_neng_ma").get("map");

            Map<String, String> dpcOpcMap = getMiddleMap("dpc_opc").get("map");
            dpcOpcMap = InterpretResultsUtil.sortMapByValueStrObj(dpcOpcMap);

            //去重的dpcAndOpc
            Map<String, String> dpcOpcMapDuplicateRemoval = getMiddleMapDuplicateRemoval("dpc_opc").get("map");
            dpcOpcMapDuplicateRemoval = InterpretResultsUtil.sortMapByKeyStrStr(dpcOpcMapDuplicateRemoval);

            Map<String,Map<String,String>> BSCLacZMap = getMiddleMap("lac_ci");
            Map<String, String> BSCLacMap = BSCLacZMap.get("map");
            Map<String, String> BSCLacFanMap = BSCLacZMap.get("fanMap");
            Map<String,Map<String,String>> lacCiMap = getKeyMap("lac_ci");

            Map<String, String> lacMap = lacCiMap.get("name1Map");
            lacMap = InterpretResultsUtil.sortMapByValueStrObj(lacMap);

            Map<String, String> ciNMap = lacCiMap.get("name2Map");
            ciNMap = InterpretResultsUtil.sortMapByValueStrObj(ciNMap);

            Map<String,Map<String,String>> BSCXiaoQuMap = getSonMap("lac_ci");

            Map<String, String> BSCMap = BSCXiaoQuMap.get("fanMap1");
            BSCMap = InterpretResultsUtil.sortMapByKeyStrStr(BSCMap);

            Map<String, String> xiaoQuMap = BSCXiaoQuMap.get("fanMap2");
            xiaoQuMap = InterpretResultsUtil.sortMapByKeyStrStr(xiaoQuMap);

            codingMap.put("zhongDuanMap",zhongDuanMap);
            codingMap.put("ascMaBiaoMap",ascMaBiaoMap);
            codingMap.put("huJiaoLeiXingMap",huJiaoLeiXingMap);
            codingMap.put("duanHaoMaMap",duanHaoMaMap);
            codingMap.put("gongNengMaMap",gongNengMaMap);
            codingMap.put("dpcOpcMap",dpcOpcMap);
            codingMap.put("dpcOpcMapDuplicateRemoval",dpcOpcMapDuplicateRemoval);

            codingMap.put("BSCMap",BSCMap);
            codingMap.put("xiaoQuMap",xiaoQuMap);
            codingMap.put("lacMap",lacMap);
            codingMap.put("ciNMap",ciNMap);
            codingMap.put("BSCLacMap",BSCLacMap);
            codingMap.put("BSCLacFanMap",BSCLacFanMap);
            mapNum = 1;
        }
    }

    @Override
    public Map<String, String> getMap(String nameKey){
        setMap();
        return codingMap.get(nameKey);
    }

    @Override
    public Map<String, String> getYingSheData(String nameKey) {
        Map<String, String> map = new HashMap<>();
        List<YingShe> middlelist = yingSheMapper.getYingSheData(nameKey);
        for (YingShe data:middlelist) {
            map.put(data.getName(),data.getValue());
        }
        return map;
    }

    @Override
    public List<YingShe> getYingSheList(String nameKey) {
        setMap();
        return codingMap.get(nameKey).entrySet().stream().map(e -> new YingShe(e.getKey(), e.getValue())).collect(Collectors.toList());
    }

    @Override
    public List<YingShe> getYingShecomtreeList(String nameKey) {
        return yingSheMapper.getYingSheData(nameKey);
    }

    private Map<String,Map<String,String>> getMiddleMap(String nameKey){
        Map<String,String> map = new HashMap<>();
        Map<String,String> fanMap = new HashMap<>();
        Map<String,Map<String,String>> returnMap = new HashMap<>();
        List<YingShe> middleList = yingSheMapper.getYingSheData(nameKey);
        for (YingShe data:middleList) {
            map.put(data.getName(),data.getValue());
            fanMap.put(data.getValue(),data.getName());
        }
        returnMap.put("map",map);
        returnMap.put("fanMap",fanMap);
        return returnMap;
    }

    /**
     * 去重
     * @param nameKey
     * @return
     */
    private Map<String,Map<String,String>> getMiddleMapDuplicateRemoval(String nameKey){
        Map<String,String> map = new HashMap<>();
        Map<String,String> fanMap = new HashMap<>();
        Map<String,Map<String,String>> returnMap = new HashMap<>();
        List<YingShe> middleList = yingSheMapper.getYingSheData(nameKey);
        for (YingShe data:middleList) {
            map.put(data.getValue(),data.getName());
            fanMap.put(data.getName(),data.getValue());
        }
        returnMap.put("map",map);
        returnMap.put("fanMap",fanMap);
        return returnMap;
    }

    private Map<String,Map<String,String>> getSonMap(String nameKey){
        Map<String,String> fanMap1 = new HashMap<>();
        Map<String,String> fanMap2 = new HashMap<>();
        Map<String,Map<String,String>> returnMap = new HashMap<>();
        List<YingShe> middleList = yingSheMapper.getYingSheData(nameKey);
        List<String> valueList1 = new ArrayList<>();
        List<String> valueList2 = new ArrayList<>();
        String name;
        String name1;
        String name2;
        String value;
        String value1;
        String value2;
        for (YingShe data:middleList) {
            name = data.getName();
            value = data.getValue();
            value1 = value.substring(0,value.indexOf("-"));
            value2 = value.substring(value.indexOf("-")+1);
            //获取BSC的去重集合
            if(valueList1.contains(value1)){
                name1 = getNewPName(fanMap1,name,value1);
                fanMap1.replace(value1,name1);
            }else{
                valueList1.add(value1);
                name1 = getPName(name);
                fanMap1.put(value1,name1);
            }
            //获取小区的去重集合
            if(valueList2.contains(value2)){
                name2 = getNewPName(fanMap2,name,value2);
                fanMap2.replace(value2,name2);
            }else{
                valueList2.add(value2);
                name2 = getPName(name);
                fanMap2.put(value2,name2);
            }
        }
        returnMap.put("fanMap1",fanMap1);
        returnMap.put("fanMap2",fanMap2);
        return returnMap;
    }

    public Map<String,Map<String,String>> getKeyMap(String nameKey){
        Map<String,Map<String,String>> returnMap = new HashMap<>();
        Map<String,String> name1Map = new HashMap<>();
        Map<String,String> name2Map = new HashMap<>();
        List<YingShe> middleList = yingSheMapper.getYingSheData(nameKey);
        Set<String> name1Set = new LinkedHashSet<>();
        Set<String> name2Set = new LinkedHashSet<>();
        String name12;
        for (YingShe data:middleList) {
            name12 = data.getName();
            name1Set.add(name12.substring(0,name12.indexOf("-")));
            name2Set.add(name12.substring(name12.indexOf("-")+1));
        }
        for (String str:name1Set) {
            name1Map.put(str,str);
        }
        for (String str:name2Set) {
            name2Map.put(str,str);
        }
        returnMap.put("name1Map",name1Map);
        returnMap.put("name2Map",name2Map);
        return returnMap;
    }

    //当值第一次出现，拼接lac和ci的sql语句
    private String getNewPName(Map<String,String> fanMap,String name,String value){
        String paramV = fanMap.get(value);
        String lac = name.substring(0,name.indexOf("-"));
        String ci = name.substring(name.indexOf("-")+1);
        String lacCi = "";
        if(!paramV.contains(lac)){
            lacCi = " or uintSCgiLac = " + lac + "_";
        }
        if(!paramV.contains(ci)){
            if(lacCi.isEmpty()){
                lacCi = lacCi + "_";
            }
            lacCi = lacCi + " or uintSCgiCi = " + ci;
        }
        return paramV.replace("_",lacCi);
    }

    //当值不是第一次出现，拼接lac和ci的sql语句
    private String getPName(String name){
        String lac = name.substring(0,name.indexOf("-"));
        String ci = name.substring(name.indexOf("-")+1);
        String lacCi = " or uintSCgiLac = " + lac + "_ or uintSCgiCi = " + ci;
        return lacCi;
    }

    //当数据表被修改时调用此方法
    @Override
    public void delNameKey(String nameKey){
        mapNum=0;
    }

    @Override
    public List<YingShe> selectYingSheAll(){
        return yingSheMapper.selectYingSheAll();
    }

    /**
     * 查询所有lac_bsc静态数据
     * @return
     */
    @Override
    public List<YingShe> selectLacBscAll(){
        return yingSheMapper.selectLacBscAll();
    }

    /**
     * 根据用户所选的bsc进行查询对应的小区名
     * @param bscValueSelect 这是一个sql语句
     * @return
     */
    @Override
    public List<YingShe> selectCommunityBybscValueSelect(String bscValueSelect){
        return yingSheMapper.selectCommunityBybscValueSelect(bscValueSelect);
    }

    /**
     * 根据用户所选的LAC进行查询对应的CI
     * @param lacValueSelect 这是一个sql语句
     * @return
     */
    @Override
    public List<YingShe> selectCommunityBylacValueSelect(String lacValueSelect){
        return yingSheMapper.selectCommunityBylacValueSelect(lacValueSelect);
    }

    /**
     * 查询全部dpc opc
     * @return
     */
    @Override
    public List<YingShe> selectOpcAndDpcAll() {
        return yingSheMapper.selectOpcAndDpcAll();
    }

}